<?php

namespace App\Console\Commands;

use App\Library\Logic\ShopOrderLogic;
use App\Library\Logic\ShopOrderExtLogic;
use Illuminate\Console\Command;

class SyncOrderExtCommand extends Command
{
    /**
     * 更新订单扩展表
     *
     * @var string
     */
    protected $signature = 'command:orderExt';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Execute the console command.
     */
    public function handle(): void
    {
        ShopOrderLogic::query()->orderByDesc('order_id')->chunk('100', function ($data) {
            foreach ($data as $value) {
                $ext = ShopOrderExtLogic::query()->where(['order_id' => $value->order_id])->first();

                if ((int)$value->order_buy_status === 6) $status = 4;
                else if ($value->order_buy_status === 1) $status = 2;
                else if ($value->order_buy_status === 2) $status = 1;
                else if ($value->order_buy_status === 3) $status = 0;
                else if ($value->order_buy_status === 4) $status = 3;
                else if ($value->order_buy_status === 5) $status = 4;
                else $status = 0;

                $arr = [
                    'status' => $status,
                    'status_cancel' => (int)$value->order_buy_status === 3 ?? 1,
                    'status_pay' => (int)$value->order_status === 1 ?? 1,
                    'status_inventory' => (is_null($value->orderReturn) || $value->orderReturn->or_api_status !== 'SUCCESS'),
                    'status_deliver' => (int)$value->order_delivery_time !== 0 ?? 1,
                    'status_refund' => (!is_null($value->orderReturn) && $value->orderReturn->or_api_status === 'SUCCESS'),
                    'refund_price' => (!is_null($value->orderReturn) && $value->orderReturn->or_api_status === 'SUCCESS') ? $value->orderReturn->or_real_price : 0,
                    'status_complete' => (int)$value->order_buy_status === 5 ?? 1,
                    'user_deleted_at' => (int)$value->order_is_delete === 1 ? date('Y-m-d H:i:s', $value->updated_at) : NULL,
                    'cancel_at' => (int)$value->order_cancel_time === 0 ? NULL : date('Y-m-d H:i:s', $value->order_cancel_time),
                    'pay_at' => (int)$value->order_pay_time === 0 ? NULL : date('Y-m-d H:i:s', $value->order_pay_time),
                    'refund_at' => is_null($value->orderReturn) || (int)$value->orderReturn->or_api_time === 0 ? NULL : date('Y-m-d H:i:s', $value->orderReturn->or_api_time),
                    'complete_at' => (int)$value->order_confirm_time === 0 ? NULL : date('Y-m-d H:i:s', $value->order_confirm_time),
                    'deliver_at' => (int)$value->order_delivery_time === 0 ? NULL : date('Y-m-d H:i:s', $value->order_delivery_time),
                    'confirm_at' => (int)$value->order_confirm_time === 0 ? NULL : date('Y-m-d H:i:s', $value->order_confirm_time),
                    'user_remark' => (int)$value->order_note === 0 ? '' : $value->order_note,
                    'status_pickup' => (int)$value->order_is_self_pickup === 2 ? 1 : 0,
                ];

                if (is_null($value->orderReturn)) $arr['status_after_sale'] = 0;
                else if ((int)$value->orderReturn->or_sell_status === 1) $arr['status_after_sale'] = 1;
                else if ((int)$value->orderReturn->or_sell_status === 2) $arr['status_after_sale'] = 2;
                else if ((int)$value->orderReturn->or_sell_status === 3) $arr['status_after_sale'] = 3;

                if (empty($ext)) ShopOrderExtLogic::insert($arr);
                else ShopOrderExtLogic::query()->where('order_id', $value->order_id)->update($arr);
            }
        });
    }
}
